package utility.geometry;

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.Point;
import entity.geometry.GroupXzqh;
import entity.geometry.MultiPolygonRing;
import entity.geometry.MultiPolygonStreetArea;
import entity.geometry.PolygonXzqh;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理GeoJSON文件
 * @date 2018/3/12.
 * @author Li Tong
 */
public class ProcessGeoJson {
    public static Map<Integer, GroupXzqh> importGeoJSONFile(String filePath){
        Logger logger = LoggerFactory.getLogger(ProcessGeoJson.class);
        FeatureJSON featureJSON = new FeatureJSON();
        try{
            FileInputStream fileInputStream = new FileInputStream(filePath);
            SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featureJSON.readFeatureCollection(fileInputStream);
            SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
            Map<Integer, GroupXzqh> polygonMaps = new HashMap<Integer, GroupXzqh>();

            while (simpleFeatureIterator.hasNext()){

                SimpleFeature simpleFeature = simpleFeatureIterator.next();
                int typeid = Integer.valueOf(simpleFeature.getAttribute("typeid").toString());
                if( polygonMaps.get(typeid) == null ){
                    GroupXzqh groupXzqh = new GroupXzqh(typeid);
                    polygonMaps.put(typeid, groupXzqh);
                }
                Long areaid = Long.valueOf(simpleFeature.getAttribute("code").toString());
                Polygon geometry = (Polygon) simpleFeature.getAttribute("geometry");

                PolygonXzqh polygonXzqh = new PolygonXzqh();
                polygonXzqh.setTypeid(typeid);
                polygonXzqh.setAreaid(areaid);
                polygonXzqh.setGeometry(geometry);

                polygonMaps.get(typeid).add(polygonXzqh);
            }
            return polygonMaps;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static ArrayList<MultiPolygonStreetArea> importStreetAreaGeojsonFile(String filePath){

        FeatureJSON featureJSON = new FeatureJSON();
        try{
            FileInputStream fileInputStream = new FileInputStream(filePath);
            SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featureJSON.readFeatureCollection(fileInputStream);
            SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
            ArrayList<MultiPolygonStreetArea> multiPolygonStreetAreasArray = new ArrayList<>();

            while (simpleFeatureIterator.hasNext()){
                SimpleFeature simpleFeature = simpleFeatureIterator.next();

                MultiPolygon multiPolygon = (MultiPolygon) simpleFeature.getAttribute("geometry");
                String streetID = simpleFeature.getAttribute("street").toString();
                String areaID = simpleFeature.getAttribute("district").toString();
                MultiPolygonStreetArea multiPolygonStreetArea = new MultiPolygonStreetArea();
                multiPolygonStreetArea.setAreaID(areaID);
                multiPolygonStreetArea.setStreetID(streetID);
                multiPolygonStreetArea.setMultiPolygon(multiPolygon);

                multiPolygonStreetAreasArray.add(multiPolygonStreetArea);
            }
            return multiPolygonStreetAreasArray;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static ArrayList<MultiPolygonRing> importRingGeojsonFile(String filePath){
        Logger logger = LoggerFactory.getLogger(ProcessGeoJson.class);
        FeatureJSON featureJSON = new FeatureJSON();
        try{
            FileInputStream fileInputStream = new FileInputStream(filePath);
            SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featureJSON.readFeatureCollection(fileInputStream);
            SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
            ArrayList<MultiPolygonRing> multiPolygonRingArray = new ArrayList<>();

            while (simpleFeatureIterator.hasNext()){
                SimpleFeature simpleFeature = simpleFeatureIterator.next();

                MultiPolygon multiPolygon = (MultiPolygon) simpleFeature.getAttribute("geometry");
                String ringID = simpleFeature.getAttribute("Id").toString();
                MultiPolygonRing multiPolygonRing = new MultiPolygonRing();
                multiPolygonRing.setRingID(ringID);
                multiPolygonRing.setRingMultiPolygon(multiPolygon);

                multiPolygonRingArray.add(multiPolygonRing);
            }
            return multiPolygonRingArray;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
